#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Author:     feng minqiang  <wnfmq@126.com>
# Maintainer: feng minqiang  <wnfmq@126.com>

import os,sys
import config
import re

_web=['ucdisk','ucdm','ucsolr','ucindexer','ucbp']
_end={'andriod':'apk','mac':'dmg','pc':'exe','ios':'ipa','web':'war'}
_ver='^\d+\.\d+\.\d+.\d+$'
r=re.compile(_ver)
def check_ver(_xiangmu=None,_project=None,_version=None):
    if _version == None:
        return False
    if _project == None:
        return False
    if _xiangmu == None:
        return False
    _filepath=config.filepath+_xiangmu+'/'+_project+'/'+ _version+'/'
    _filename='ucdisk_'+_xiangmu+'_'+_project+'_'+_version
    _filename=_filename+"."+_end[_project]
    #print _filepath,_filename
    _file=[]
    if _project == 'web':
        for i in _web:
            _filename=i+'.war'
            _file.append(_filename)
    else:
        _file.append(_filename)
    for i in _file:
        if not os.path.isfile(_filepath+i):
            #print _filepath+i
            return False
    return True

def sort_ver(_version_list=[]):
    if _version_list==[]:
        return _version_list
    _tmp_version=[]
    z=1
    while z < len(_version_list):
        #print "diff :" ,_version_list[z-1],_version_list[z],diff_ver(_version_list[z-1],_version_list[z])
        if diff_ver(_version_list[z-1],_version_list[z])==1:
            _tmp=_version_list[z]
            f=z;
            while f > 0 and diff_ver(_version_list[f-1],_tmp)==1:
                _version_list[f]=_version_list[f-1]
                f=f-1
                _version_list[f]=_tmp
        z=z+1
    return  _version_list
    
def diff_ver(_version1=None,_version2=None):
    if _version1 == None or _version2==None:
        return None
    if r.match(_version1) == None or r.match(_version2) ==None:
        return None
    _i1=_version1.split('.')
    _i2=_version2.split('.')
    if int(_i1[0])<int(_i2[0]):
        return -1
    elif int(_i1[0])>int(_i2[0]):
        return 1
    elif int(_i1[1]) < int(_i2[1]):
        return -1
    elif int(_i1[1]) > int(_i2[1]):
        return 1
    elif int(_i1[2]) < int(_i2[2]):
        return -1
    elif int(_i1[2]) > int(_i2[2]):
        return 1
    elif int(_i1[3]) < int(_i2[3]):
        return -1
    elif int(_i1[3]) >int(_i2[3]):
        return 1
    elif int(_i1[3]) ==int(_i2[3]):
        return 0
    else:
        #return None
        pass
    
def get_ver(_xiangmu=None,_project=None):
    if _project == None:
        return False
    if _xiangmu == None:
        return False
    _filepath=config.filepath + _xiangmu + '/' + _project
    #print _filepath
    if not os.path.exists(_filepath):
        return None
    _tmp_file_list=[]
    _tmp_path_list=[]
    for rt,dirs,files in os.walk(_filepath):
        #print rt,dirs,files
        for i in dirs:
            if r.match(i) != None:
                #print i
                _tmp_path_list.append(i)
        #print _tmp_path_list
        #_tmp_path_list=['3.0.5.10','2.0.5.5', '3.0.5.5','3.0.5.3', '3.0.5.1', '3.0.5.2','3.0.5.1','3.0.5.16','3.0.5.1','2.0.5.4']
        _tmp_path_list=sort_ver(_tmp_path_list)
        #print _tmp_path_list
        break
    i=len(_tmp_path_list)
    while i>0:
        i=i-1
        _max=_tmp_path_list[i]
        if check_ver(_xiangmu,_project,_max):
            return _max
    return  None


    
    
if __name__ == '__main__':
    check_ver("hdjy",'andriod','3.0.5.3');
    print get_ver("hdjy",'andriod')

